<template>
  <div class="text-tools-page">
    <div class="container">
      <div class="page-header">
        <h1 class="page-title">文本处理工具</h1>
        <p class="description">
          提供多种文本处理工具，帮助您轻松进行字数统计、大小写转换、文本格式化等操作
        </p>
      </div>
      
      <div class="tools-navigation">
        <el-tabs 
          v-model="selectedTool" 
          class="tool-tabs"
          @tab-change="handleToolChange"
          type="card"
        >
          <el-tab-pane 
            v-for="tool in tools" 
            :key="tool.value" 
            :label="tool.label" 
            :name="tool.value"
          >
            <template #label>
              <div class="tab-label">
                <el-icon><component :is="tool.icon" /></el-icon>
                <span>{{ tool.label }}</span>
                <span v-if="!tool.available" class="coming-soon-badge">开发中</span>
              </div>
            </template>
          </el-tab-pane>
        </el-tabs>
      </div>
      
      <div class="tool-container">
        <!-- 工具组件动态加载 -->
        <component 
          :is="currentToolComponent" 
          v-if="currentToolComponent"
          @tool-action="handleToolAction"
        />
        <el-empty v-else description="工具加载中" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, markRaw, defineAsyncComponent, onMounted, nextTick } from 'vue';
import { 
  DocumentChecked, 
  SortUp, 
  SetUp
} from '@element-plus/icons-vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';

// 懒加载组件
const TextCounter = defineAsyncComponent(() => import('../../components/tools/text/TextCounter.vue'));
const CaseConverter = defineAsyncComponent(() => import('../../components/tools/text/CaseConverter.vue'));
const TextFormatter = defineAsyncComponent(() => import('../../components/tools/text/TextFormatter.vue'));

const route = useRoute();
const router = useRouter();
const store = useStore();

// 工具列表
const tools = [
  { 
    label: '字数统计', 
    value: 'counter', 
    component: markRaw(TextCounter),
    icon: 'DocumentChecked',
    available: true
  },
  { 
    label: '大小写转换', 
    value: 'case-converter', 
    component: markRaw(CaseConverter),
    icon: 'SortUp',
    available: true
  },
  { 
    label: '文本格式化', 
    value: 'formatter', 
    component: markRaw(TextFormatter),
    icon: 'SetUp',
    available: true
  }
];

// 当前选择的工具
const selectedTool = ref('counter');

// 根据工具值获取对应组件
const getToolComponent = (toolValue) => {
  const tool = tools.find(t => t.value === toolValue);
  return tool && tool.available ? tool.component : null;
};

// 当前工具组件
const currentToolComponent = computed(() => {
  return getToolComponent(selectedTool.value);
});

// 工具切换处理
const handleToolChange = (toolValue) => {
  selectedTool.value = toolValue;
  
  // 更新URL参数但不导航
  const query = { ...route.query, tool: toolValue };
  router.replace({ query });
  
  // 跟踪工具使用情况
  store.dispatch('trackToolUsage', {
    category: 'text',
    tool: toolValue,
    timestamp: new Date().toISOString()
  });
  
  // 如果工具不可用，显示提示
  const tool = tools.find(t => t.value === toolValue);
  if (tool && !tool.available) {
    ElMessage.info(`${tool.label}功能正在开发中，即将上线！`);
  }
};

// 处理工具操作
const handleToolAction = (action) => {
  console.log('工具操作:', action);
  
  // 可以根据不同工具的操作进行处理
  if (action.type === 'success') {
    ElMessage.success(action.message || '操作成功');
  } else if (action.type === 'error') {
    ElMessage.error(action.message || '操作失败');
  }
};

// 生命周期钩子
onMounted(async () => {
  // 从URL参数中获取工具
  if (route.query.tool && tools.some(t => t.value === route.query.tool)) {
    selectedTool.value = route.query.tool;
  }
  
  // 页面加载完成后更新工具使用记录
  await nextTick();
  store.dispatch('incrementPageView', {
    page: 'TextToolsView',
    timestamp: new Date().toISOString()
  });
});
</script>

<style scoped>
.text-tools-page {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  margin-bottom: 30px;
  text-align: center;
}

.page-title {
  font-size: 28px;
  color: #303133;
  margin-bottom: 10px;
}

.description {
  font-size: 16px;
  color: #606266;
  max-width: 700px;
  margin: 0 auto;
}

/* 工具导航栏样式 */
.tools-navigation {
  margin-bottom: 32px;
  border-radius: 8px;
  background-color: #f5f7fa;
  padding: 4px;
}

.tool-tabs {
  width: 100%;
}

:deep(.el-tabs__nav) {
  width: 100%;
  display: flex;
}

:deep(.el-tabs__item) {
  flex: 1;
  text-align: center;
  height: 56px;
  transition: all 0.3s;
}

:deep(.el-tabs--card > .el-tabs__header .el-tabs__item) {
  border: none;
  border-radius: 8px;
  margin: 4px;
  background-color: transparent;
}

:deep(.el-tabs--card > .el-tabs__header .el-tabs__item.is-active) {
  background-color: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  color: #3498db;
}

:deep(.el-tabs--card > .el-tabs__header) {
  border: none;
}

.tab-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  position: relative;
}

.tab-label .el-icon {
  font-size: 20px;
}

.coming-soon-badge {
  position: absolute;
  top: -6px;
  right: -10px;
  background-color: #e6a23c;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
  font-weight: bold;
  transform: scale(0.85);
}

.tool-container {
  max-width: 1200px;
  margin: 0 auto;
  min-height: 400px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .tab-label {
    flex-direction: row;
    gap: 8px;
  }
  
  :deep(.el-tabs__item) {
    height: 40px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .description {
    font-size: 14px;
  }
}
</style> 